package com.cheetah.dao.impl;

import com.cheetah.dao.GroupDAO;
import com.cheetah.entity.*;
import com.cheetah.exceptions.CheetahRuntimeException;
import com.cheetah.support.redis.RedisClient;
import com.cheetah.utils.KeyUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.BulkOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import static com.cheetah.constants.KeyConstants.*;

/**
 * @Author Administrator
 * @Date 2020/8/25
 * @Version V1.0
 * @Description:
 **/
@Repository
@Slf4j
public class GroupDAOImpl implements GroupDAO {

    @Autowired
    @Qualifier("REDIS_IM")
    private RedisClient redisClient;

    @Autowired
    @Qualifier("MONGO_IM")
    private MongoTemplate mongoTemplate;

    @Override
    public void createGroup(Group group){
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            redisClient.set(KEY_GROUP_S,objectMapper.writeValueAsString(group));
        } catch (JsonProcessingException e) {
            throw new CheetahRuntimeException(e.getMessage(),e);
        }
        mongoTemplate.save(group);
    }

    @Override
    public void  addGroupUser(List<String> list, String groupId){
        redisClient.sAdd(KeyUtil.generate(KEY_GROUP_USER_S,groupId),list.toArray());

        List<GroupUser> groupUsers = new ArrayList<>();
        list.forEach(u -> {
            groupUsers.add(GroupUser.builder()
                    .groupId(groupId)
                    .userId(u)
                    .build()
            );
        });

        mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, GroupUser.class).insert(groupUsers);
    }

    @Override
    public void addUserGroupList(String userId, String groupId, String networkId){
        long score = System.currentTimeMillis();
        redisClient.zSet(KeyUtil.generate(networkId,KEY_USER_GROUP_LIST,userId),groupId,score);

        UserGroupList userGroupList = UserGroupList.builder()
                .networkId(networkId)
                .userId(userId)
                .groupId(groupId)
                .score(score)
                .build();
        mongoTemplate.save(userGroupList);
    }

    @Override
    public void addGroupMessage(String groupId, Message message){
        redisClient.zSet(KeyUtil.generate(KEY_GROUP_MSG,groupId),
                String.valueOf(message.getId()),
                message.getCreateTime().getTime());

        GroupMessage groupMessage = GroupMessage.builder()
                .groupId(groupId)
                .msgId(message.getId())
                .score(message.getId())
                .build();
        mongoTemplate.save(groupMessage);
    }
}
